package com.ahsjdy.st.stbomanagerweb.controller;

import com.ahsjdy.st.stservice.service.UserService;
import com.ahsjdy.st.stservice.service.impl.CustomerService;
import com.ahsjdy.st.stservice.service.impl.NavServiceImpl;
import com.ahsjdy.stcommon.dto.ResponseDTO;
import com.ahsjdy.stcommon.vo.SettleRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private NavServiceImpl navService;

    @Autowired
    private UserService userService;

    @RequestMapping("/customer/wxLogin")
    public ResponseDTO wxLogin(String code) {
        return ResponseDTO.success(userService.wxLogin(code));
    }

    @RequestMapping("/customer/uploadPic")
    public ResponseDTO uploadPic(MultipartFile uploadFile) {
        if (uploadFile.isEmpty()) {
            return ResponseDTO.failed("请选择图片");
        }

        try {
            String picName = UUID.randomUUID().toString().replaceAll("-", "");
            uploadFile.transferTo(new File(filePath + picName));

            Map<String, String> map = new HashMap();
            map.put("picName", picName);
            map.put("picUrl", fileUrl + picName);
            return ResponseDTO.success(map);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseDTO.failed("上传失败");
        }
    }

    @RequestMapping("/customer/uploadPic/{picName}")
    public void showPic(@PathVariable("picName") String picName, HttpServletResponse response) {
        try (FileInputStream bis = new FileInputStream(filePath + picName);) {
            ServletOutputStream outputStream = response.getOutputStream();

            byte[] buff = new byte[10240];
            int length = -1;
            while (-1 != (length = bis.read(buff))) {
                outputStream.write(buff, 0, length);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户下单
     * @return
     */
    @RequestMapping(value = "/customer/junkOrder")
    public ResponseDTO junkOrder(SettleRequestVO settleRequestVO) {
        customerService.settle(settleRequestVO);
        return ResponseDTO.success();
    }

    @RequestMapping("/customer/listNav")
    public ResponseDTO listNav() {
        return ResponseDTO.success(navService.listNav());
    }


    @RequestMapping("/customer/loadOrder")
    public ResponseDTO loadOrder(String openid) {
        return ResponseDTO.success(customerService.queryOrders(openid));
    }

    @RequestMapping("/customer/overOrder")
    public ResponseDTO overOrder(String openid, int orderId) {
        customerService.overOrder(openid, orderId);
        return ResponseDTO.success();
    }


    @RequestMapping(value = "/customer/wxBindPhone", method = RequestMethod.POST)
    public ResponseDTO wxBindPhone(String phone, String openId) {
        userService.wxBindPhone(openId, phone);
        return ResponseDTO.success();
    }
}
